package playground.fibu.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import playground.fibu.ejb.AccountBO;
import playground.fibu.ejb.BookingBO;
import playground.fibu.ejb.dao.FibuDAOException;
import playground.fibu.ejb.entity.Account;

@ManagedBean
@SessionScoped
public class FibuBean {

	private BookingBO booking;
	private List<BookingBO> bookingList;
	private List<SelectItem> accountList;

	public FibuBean() {
		booking = new BookingBO();
		bookingList = new ArrayList<BookingBO>();
		accountList = new ArrayList<SelectItem>();
		refreshData();
	}

	public String save() {
		try {
			booking.save();
			refreshData();

		} catch (FibuDAOException e) {
			System.out.println("ERROR");
			e.printStackTrace();
		}
		return null;
	}

	public String showAccounts() {
		return "accounts";
	}

	private void refreshData() {
		List<BookingBO> bookings;
		try {
			bookingList = BookingBO.findAll();
			accountList = new ArrayList<SelectItem>();
			for (Account a : AccountBO.findAll()) {
				accountList.add(new SelectItem(a, a.getAccountName()));
			}

		} catch (FibuDAOException e) {
			e.printStackTrace();
		}
	}

	public double getAmount() {
		return booking.getAmount();
	}

	public void setAmount(double amount) {
		booking.setAmount(amount);
	}

	public String getText() {
		return booking.getText();
	}

	public void setText(String text) {
		booking.setText(text);
	}

	public List<BookingBO> getBookingList() {
		return bookingList;
	}

	public List<SelectItem> getAccountList() {
		return accountList;
	}

	public Account getSelectedAccount() {
		return booking.getDebitAccount();
	}

	public void setSelectedAccount(Account selectedAccount) {
		booking.setDebitAccount(selectedAccount);
	}

}
